/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driving.persistence.user;

import com.lap.auth.adapter.driving.persistence.user.po.UserPo;
import com.lap.auth.domain.user.User;
import com.lap.auth.domain.user.valueobject.Status;
import com.lap.auth.domain.user.valueobject.UserType;
import com.lap.framework.common.tool.EnumUtil;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface UserConverter {

  UserPo toPo(User user);

  /**
   * 小细节:如果字段是枚举,只需要定义方法，不需要标注mapping
   *
   * <ol>
   *   <li>方法中标注:@Named("codeToStatus")
   *   <li>转换中:@Mapping(target = "status", source = "status", qualifiedByName = "codeToStatus")
   * </ol>
   *
   * @param userPo 用户数据模型
   * @return 用户领域模型
   */
  @Mapping(target = "event", ignore = true)
  User toDomain(UserPo userPo);

  default Status codeToStatus(Integer code) {
    return EnumUtil.fromCode(Status.class, code);
  }

  default Integer statusToCode(Status status) {
    return status.getCode();
  }

  default UserType codeToUserType(Integer code) {
    return EnumUtil.fromCode(UserType.class, code);
  }

  default Integer userTypeToCode(UserType type) {
    return type.getCode();
  }
}
